package com.djzhou.gmms.user.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import jodd.util.StringUtil;

import com.djzhou.gmms.base.action.BaseAction;
import com.djzhou.gmms.model.company.TbCompany;
import com.djzhou.gmms.model.department.TbDepartment;
import com.djzhou.gmms.model.position.TbPosition;
import com.djzhou.gmms.model.role.TbRole;
import com.djzhou.gmms.model.user.TbUser;
import com.djzhou.gmms.model.user.VoResource;
import com.djzhou.util.MD5;
import com.opensymphony.xwork2.ModelDriven;

public class UserAction extends BaseAction<TbUser> implements ModelDriven<TbUser> {

	private static final long serialVersionUID = 1L;
	private Integer userId;
	private TbUser user;
	private List<TbUser> list;
	private List<TbRole> roleList;
	private List<TbDepartment> departmentList;
	private List<TbCompany> companyList; 
	private List<TbPosition> positionList;
	private Integer departmentId;
	private Integer areaId;
	private String userName;
	private Integer positionId;
	private String roleIds;
	private Integer mainRoleId;
	private List<TbRole> haveRoleList;//用户已拥有角色
	private String password;
	private String loginName;
	private Integer roleId;
	
	
	public String queryAllUserList(){
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		if(departmentId!=null)hashMap.put("departmentId", departmentId);
		if(StringUtil.isNotEmpty(userName))hashMap.put("userName", userName);
		list=userService.queryList(hashMap);
		return SUCCESS;
	}
	
	public String verifyUser(){
		if(userService.verifyUserIsExists(loginName, userId)){
			successFlag=1;
		}else{
			successFlag=0;
		}
		return SUCCESS;
	}

	@Override
	public String delete() {
		userService.delete(userId);
		msg="删除成功";
		setCurrentPage(currentPage);
		return SUCCESS;
	}

	@Override
	public String modify() {
		TbDepartment department=departmentService.queryById(departmentId);
		user.setDepartment(department);
		user.setCompany(department.getCompany());
		user.setLastModifyTime(new Date());
		user.setExpressPwd(user.getLoginPwd());
		user.setLoginPwd(MD5.toMD5(user.getLoginPwd()));
		user.setDeleteFlag(0);
		user.setOpUser(queryUser().getUserId());
		user.setPosition(positionService.queryById(positionId));
		user.setMainRole(roleService.queryById(mainRoleId));
		userService.update(user);
		userRoleService.update(roleIds,user.getUserId(),mainRoleId);
		msg="修改成功";
		setCurrentPage(currentPage);

		return SUCCESS;
	}

	@Override
	public String queryList() {
		if(loadData==null){
			departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
			return SUCCESS;
		}
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		if(departmentId!=null && !departmentId.equals("1") && !departmentId.equals(""))hashMap.put("departmentId", departmentId);
		if(StringUtil.isNotEmpty(userName))hashMap.put("userName", userName);
		hashMap.put("companyId", queryCompany().getCompanyId());
		pageBean=userService.queryPageBean(hashMap, pageSize, currentPage);
		list=pageBean.getList();
		if(currentPage>1){
			if(list==null || list.size()==0){
				setCurrentPage(getCurrentPage()-1);
				pageBean=userService.queryPageBean(hashMap, pageSize, currentPage);
				list=pageBean.getList();
			}
		}
		if(list!=null){
			totalCount=pageBean.getAllRow();
		}else{
			totalCount = 0;
			list = new ArrayList<TbUser>();
		}
		totalPage=pageBean.getTotalPage();
		setCurrentPage(currentPage);
		setDepartmentId(departmentId);
		setUserName(userName);
		return SUCCESS;
	}

	public String query4AutoComplete(){
		HashMap<String, Object> hashMap=new HashMap<String, Object>();
		if(StringUtil.isNotEmpty(searchStr))hashMap.put("userName", searchStr);
		if(areaId!=null)hashMap.put("areaId", areaId.toString());
		if(StringUtil.isNotEmpty(roleIds))hashMap.put("roleIds", roleIds);
		resource=userService.query4AutoComplete(hashMap,sidx,sord,rows,page);
		return SUCCESS;
	}
	
	@Override
	public String save() {
		if(user!=null){
			TbDepartment department=departmentService.queryById(departmentId);
			user.setDepartment(department);
			Date date=new Date();
			user.setExpressPwd(user.getLoginPwd());
			user.setLoginPwd(MD5.toMD5(user.getLoginPwd()));
			user.setCompany(department.getCompany());
			user.setUserNo(userService.queryUserNo());
			user.setUserCreateWay("管理员添加");
			user.setOpUser(super.queryUser().getUserId());
			user.setCreateTime(date);
			user.setLastModifyTime(date);
			user.setDeleteFlag(0);
			user.setPosition(positionService.queryById(positionId));
			user.setMainRole(roleService.queryById(mainRoleId));
			userService.add(user);
			userRoleService.add(roleIds,user.getUserId(),mainRoleId);
		}
		return SUCCESS;
	}

	@Override
	public String toAdd() {
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		roleList=roleService.queryList(queryCompany().getCompanyId());
		positionList=positionService.queryList(queryCompany().getCompanyId());
		if(queryUser().getUserId()==1)companyList=companyService.queryList(1);
		return SUCCESS;
	}

	@Override
	public String toEdit() {
		roleIds=userRoleService.queryRoleIdsByUserId(userId);
		departmentList=departmentService.queryList(queryCompany().getCompanyId(),0);//查询当前用户所在公司下的部门
		user=userService.queryById(userId);
		roleList=roleService.queryList(queryCompany().getCompanyId());
		positionList=positionService.queryList(queryCompany().getCompanyId());
		if(queryUser().getUserId()==1)companyList=companyService.queryList(1);
		haveRoleList=userRoleService.queryRoleListByUserId(userId);
		TbRole role =userRoleService.queryMainRoleByUserId(userId);
		mainRoleId=role==null ? -1:role.getRoleId();
		return SUCCESS;
	}
	
	public String changePassword(){
		user=userService.queryById(userId);
		user.setExpressPwd(password);
		password=MD5.toMD5(password);
		user.setLoginPwd(password);
		userService.update(user);
		ServletActionContext.getRequest().getSession().setAttribute("sessionUser", user);
		msg="密码修改成功!";
		return SUCCESS;
	}
	
	public String queryUpdateLogVersion(){
		if(queryUser().getUpdateLogVersion()!=0)successFlag=1;
		else successFlag=0;
		return SUCCESS;
	}

	public TbUser getModel() {
		return user;
	}

	public Integer getUserId() {
		return userId;
	}

	public void setUserId(Integer userId) {
		this.userId = userId;
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	public List<TbUser> getList() {
		return list;
	}

	public void setList(List<TbUser> list) {
		this.list = list;
	}

	public List<TbRole> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<TbRole> roleList) {
		this.roleList = roleList;
	}

	public List<TbDepartment> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<TbDepartment> departmentList) {
		this.departmentList = departmentList;
	}

	public Integer getDepartmentId() {
		return departmentId;
	}

	public void setDepartmentId(Integer departmentId) {
		this.departmentId = departmentId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getPositionId() {
		return positionId;
	}

	public void setPositionId(Integer positionId) {
		this.positionId = positionId;
	}

	public VoResource getResource() {
		return resource;
	}

	public void setResource(VoResource resource) {
		this.resource = resource;
	}

	public List<TbPosition> getPositionList() {
		return positionList;
	}

	public void setPositionList(List<TbPosition> positionList) {
		this.positionList = positionList;
	}

	public List<TbCompany> getCompanyList() {
		return companyList;
	}

	public void setCompanyList(List<TbCompany> companyList) {
		this.companyList = companyList;
	}

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}

	public Integer getMainRoleId() {
		return mainRoleId;
	}

	public void setMainRoleId(Integer mainRoleId) {
		this.mainRoleId = mainRoleId;
	}

	public List<TbRole> getHaveRoleList() {
		return haveRoleList;
	}

	public void setHaveRoleList(List<TbRole> haveRoleList) {
		this.haveRoleList = haveRoleList;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public Integer getRoleId() {
		return roleId;
	}

	public void setRoleId(Integer roleId) {
		this.roleId = roleId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Integer getAreaId() {
		return areaId;
	}

	public void setAreaId(Integer areaId) {
		this.areaId = areaId;
	}
	
	
	

}
